<template>
    <div class="container">
        <!-- header -->
        <div class="header">
            <div class="header-box">
                <p class="header-title">标签分类:</p>
                <el-input
                    v-model="keword"
                    suffix-icon="el-icon-search"
                    size="small"
                    clearable
                    class="cont-ipt"
                    @change="getTableData(1)"
                    @keyup.native.enter="getTableData(1)"
                />
                <p class="header-title header-left">来源:</p>
                <el-select
                    v-model="source"
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in sourceList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
                <p class="header-title header-left">类型:</p>
                <el-select
                    v-model="type"
                    filterable
                    placeholder="请选择"
                    size="small"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in typeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <el-button
                type="primary"
                size="small"
                @click="fnCreatLabel"
                class="header-label"
            >新建分类</el-button>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
        >
            <el-table-column
                prop="group_no"
                label="编号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                prop="target_type"
                label="类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.target_type == 1">用户</span>
                    <span v-else-if="scope.row.target_type == 2">商品</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="can_delete"
                label="来源"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.can_delete == 0 ">机器</span>
                    <span v-else-if="scope.row.can_delete == 1 ">人工</span>
                    <span v-else>--</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="name"
                label="标签分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
            />
            <el-table-column
                label="操作"
                fixed="right"
            >
                <template
                    slot-scope="scope"
                >
                    <span
                        class="curse-forb"
                        @click="fnEditLabel(scope.row)"
                        v-if="scope.row.can_delete == 1"
                    >编辑</span>
                    <span
                        class="curse-forb"
                        @click="fnDeleteLabel(scope.row)"
                        v-if="scope.row.can_delete == 1"
                    >删除</span>
                    <span v-if="scope.row.can_delete !== 1">--</span>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- dialog -->
        <el-dialog
            :visible.sync="centerDialogVisible"
            width="450px"
            center
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="选择类型"
                    prop="ruleFormtype"
                >
                    <el-button
                        :type="ruleForm['type'] == 1 ? 'primary' : '' "
                        size="small"
                        class="header-label"
                        @click="ruleForm['type'] = 1 "
                    >用户</el-button>
                    <el-button
                        :type="ruleForm['type'] == 1 ? '' : 'primary' "
                        size="small"
                        class="header-label"
                        @click="ruleForm['type'] = 2 "
                    >商品</el-button>
                </el-form-item>
                <el-form-item
                    label="新建分类"
                    prop="label"
                >
                    <el-input
                        v-model="ruleForm.label"
                    />
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    size="small"
                    @click="centerDialogVisible = false">取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    @click="submitForm('ruleForm')">保 存</el-button>
            </span>
        </el-dialog>
        <!-- /dialog -->


        <!-- 分页 -->
        <div
            class="pagination-wrapper clearfix"
            v-if="tableData.length > 0"
        >
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
        <!-- /分页 -->
    </div>
</template>

<script>
export default {
    data () {
        return {
            keword: '',
            source: '',
            sourceList: [{
                value: 0,
                label: '全部'
            },{
                value: 1,
                label: '人工分类'
            },{
                value: 2,
                label: '机器分类'
            }],
            type: '',
            typeList: [{
                value: 0,
                label: '全部'
            },{
                value: 1,
                label: '用户'
            },{
                value: 2,
                label: '商品'
            }],
            centerDialogVisible: false,
            tableData: [],
            ruleForm: {
                type: 1,
                label: ''
            },
            rules: {
                label: [
                    { required: true, message: '请填写分类，不可以为空', trigger: 'blur' }
                ]
            },
            modelType: '',
            group_no: '',
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            }
        };
    },
    watch: {
        //关闭弹窗情况表单内容
        centerDialogVisible (val) {
            if (!val) {
                this.ruleForm = {
                    type: 1,
                    label: ''
                };
                this.$refs['ruleForm'].resetFields();
            }
        },
    },
    created () {
        this.setHeader();
        this.getTableData();
    },
    mounted () {
    },
    methods: {
        /**
         * 设置表单header
         */
        setHeader () {
            this.$store.commit('SET_HEADER', [{
                title: "APP后台",
            },
            {
                title: '标签管理'
            },
            {
                title: "标签分类管理"
            }]
            );
        },

        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/tag_group%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword: this.keword,
                target_type: this.type,
                can_delete: this.source
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 新建分类
         */
        fnCreatLabel () {
            this.modelType = 'add';
            this.centerDialogVisible = !this.centerDialogVisible;
        },

        /**
         * 弹窗保存/添加标签
         */
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.modelType == 'add') { //新建
                        this.$post('/student/tag_group%5Cadd', {
                            target_type: this.ruleForm.type,
                            name: this.ruleForm.label
                        }, resp => {
                            if (resp.code == 1) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功'
                                });
                                this.getTableData();
                                this.centerDialogVisible = !this.centerDialogVisible;
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'waring',
                                    message: resp.msg
                                });
                            }
                        });
                    } else if (this.modelType == 'edit') { //编辑
                        this.$post('/student/tag_group%5Cset', {
                            group_no: this.group_no,
                            name: this.ruleForm.label
                        }, resp => {
                            if (resp.code == 1) {
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功'
                                });
                                this.getTableData();
                                this.centerDialogVisible = !this.centerDialogVisible;
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'waring',
                                    message: resp.msg
                                });
                            }
                        });
                    } else {
                        return;
                    }

                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 编辑分类
         */
        fnEditLabel (row) {
            this.modelType = 'edit';
            this.group_no = row.group_no;
            this.centerDialogVisible = !this.centerDialogVisible;
            this.ruleForm = {
                type: row.target_type,
                label: row.name
            };
        },

        /**
         * 删除分类
         */
        fnDeleteLabel (row) {
            if (row.can_delete == 1) {
                this.$confirm('此操作将永久删除该该标签分类, 是否继续?', '提示', {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$post('/student/tag_group%5Cdelete', {
                        group_no: row.group_no
                    }, resp => {
                        if (resp.code == 1) {
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            });
                            this.getTableData();
                        } else if (resp.code == 0) {
                            this.$message({
                                type: 'waring',
                                message: resp.msg
                            });
                        }
                    });
                });
            } else if (row.can_delete == 0) {
                this.$message({
                    type: 'waring',
                    message: '当前分类下有标签，不允许删除'
                });
            } else {
                return;
            }
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        .header {
            margin-bottom: 20px;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: space-between;
            .header-box {
                display: flex;
                align-items: center;
            }
            .header-left {
                margin-left: 25px;
            }
            .header-title {
                color: #606266;
                margin-right: 8px;
                white-space: nowrap;
            }
            .header-right {
                display: flex;
                margin-left: 30px;
                align-items: center;
            }
            .header-label {
                margin-right: 30px;
            }
        }
        .cont-ipt {
            width: 200px;
            margin-left: 10px;
        }
        .curse-forb {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        .user-number {
            width: 280px;
        }
        .school-wrap {
            display: flex;
            align-items: center;
            cursor: pointer;
            .school-spliter {
                width: 1px;
                height: 12px;
                background: rgba(235, 235, 235, 1);
                margin: 0 10px;
            }
            .btn-school {
                font-size: 14px;
                line-height: 1;
                color: #ccc;
                &:hover {
                    color: #20a0ff;
                }
            }
        }
        .pagination-wrapper {
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .school-name {
            display: inline-block;
            width: 150px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    }
</style>
<style lang="less">
    .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
</style>